package com.hmkj.admin.controller.user;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.admin.utils.AdminSessionHelper;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.entity.export.ExportUtils;
import com.hmkj.common.utils.ValidateUtils;
import com.hmkj.core.constant.Constants;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.model.user.UserBankModel;
import com.hmkj.core.po.system.Admin;
import com.hmkj.core.po.system.Bank;
import com.hmkj.core.po.user.UserBank;
import com.hmkj.core.service.system.BankService;
import com.hmkj.core.service.user.UserBankService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@Controller
@RequestMapping(BaseController.BASE_URI)
public class UserBankJsonController extends GenericController<Integer, UserBank> {
    @Resource
    private UserBankService userBankService;
    @Resource
    private BankService bankService;

    /**
     * 银行卡认证列表json数据
     */
    @ResponseBody
    @RequestMapping("userBank/json/list")
    @RequiresPermissions("sys:user:bank:list")
    public DtGrid listJson(String dtGridPager) throws Exception {
        DtGrid dtgrid = dataAuthority(dtGridPager, Constants.AG);
        return userBankService.getDtGridList(dtgrid, UserBankModel.class);
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping(value = "/userBank/json/edit")
    @RequiresPermissions("sys:user:bank:edit")
    public ResultEntity edit(UserBank userBank) throws Exception {
        UserBank oldUserBank = userBankService.selectOne(new UserBank(u -> {
            u.setId(userBank.getId());
            u.setDelFlag(UserBank.DELFLAG.NORMAL.code);
        }));
        if (null == oldUserBank) {
            throw new BussinessException("该银行卡信息不存在");
        }
        if(userBank.getBankCode().length()>20){
            throw new BussinessException("银行卡号超过20个字符");
        }
        if(!ValidateUtils.isNumeric(userBank.getBankCode())){
            throw new BussinessException("银行卡号格式有误");
        }
        String str  = "^[\\u4e00-\\u9fa5]+$";
        if(!userBank.getBranch().matches(str)){
            throw new BussinessException("支行名称必须中文");
        }
        Bank bank =bankService.selectOne(new Bank(t->t.setInsideCode(userBank.getBankCode())));
        userBankService.updateByPrimaryKeySelective(new UserBank(u -> {
            u.setId(oldUserBank.getId());
            u.setBankNo(userBank.getBankNo());
            u.setBankName(bank.getName());
            u.setBankCode(userBank.getBankCode());
            u.setBranch(userBank.getBranch());
        }));
        return ok();
    }

    /**
     * 审核
     */
    @ResponseBody
    @RequestMapping(value = "/userBank/json/refuse")
    @RequiresPermissions("sys:bind:audit")
    public ResultEntity refuse(UserBank userBank) throws Exception {
        UserBank oldUserBank = userBankService.selectOne(new UserBank(u -> {
            u.setId(userBank.getId());
            u.setDelFlag(UserBank.DELFLAG.NORMAL.code);
        }));
        if (null == oldUserBank) {
            throw new BussinessException("该银行卡信息不存在");
        } else if (oldUserBank.getStatus() != UserBank.STATUS.T1.code) {
            throw new BussinessException("当前状态下不能操作");
        }
        userBankService.updateByPrimaryKeySelective(new UserBank(u -> {
            u.setId(oldUserBank.getId());
            u.setStatus(userBank.getStatus());
            u.setRemark(userBank.getRemark());
        }));
        return ok();
    }

    /**
     * 列表导出
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "userBank/json/export", method = RequestMethod.POST)
    public ResultEntity export(String dtGridPager, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 执行导出
        ExportUtils.export(request, response, userBankService.getDtGridListExport(dtGridPager, UserBankModel.class));
        return ok();
    }
}
